package com.dzl.movie;

import com.dzl.base.controller.BaseController;
import com.dzl.moive.pojo.dto.WelfareMovieOrderPayDto;
import com.dzl.moive.pojo.dto.WelfareMovieOrderSaveDto;
import com.dzl.moive.pojo.query.WelfareMovieOrderQuery;
import com.dzl.moive.pojo.vo.WelfareMovieOrderPayVo;
import com.dzl.moive.pojo.vo.WelfareMovieOrderVo;
import com.dzl.moive.service.WelfareMovieOrderService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 电影订单(welfare_movie_order)表控制层
 * @author liuguanjun
 * @date 2021-11-03 17:23:29
 */
@RestController
@RequestMapping("/welfareMovieOrder")
public class WelfareMovieOrderController extends BaseController {

    @Autowired
    private WelfareMovieOrderService service;

    /**
     * 电影购票生成订单
     * @date 2021-11-04 11:28:05
     * @author liuguanjun
     * @param welfareOrderSaveDto api保存订单传参
     */
    @PostMapping("saveOrder")
    public JsonResult<Integer> saveOrder(@RequestBody WelfareMovieOrderSaveDto welfareOrderSaveDto) throws UnsupportedEncodingException {
        if(EmptyUtils.isEmpty(welfareOrderSaveDto.getOpiid())) {
            return JsonResult.paramErrWithMsg("场次ID不能为空");
        }
        if(EmptyUtils.isEmpty(welfareOrderSaveDto.getPlaytime())) {
            return JsonResult.paramErrWithMsg("放映时间不能为空");
        }
        if(EmptyUtils.isEmpty(welfareOrderSaveDto.getSeatids())) {
            return JsonResult.paramErrWithMsg("座位ID不能为空");
        }
        if(EmptyUtils.isEmpty(welfareOrderSaveDto.getSeatnames())) {
            return JsonResult.paramErrWithMsg("座位名称不能为空");
        }
        if(EmptyUtils.isEmpty(welfareOrderSaveDto.getMobile())) {
            return JsonResult.paramErrWithMsg("用户购票手机号不能为空");
        }
        if(EmptyUtils.isEmpty(welfareOrderSaveDto.getAreas())) {
            return JsonResult.paramErrWithMsg("区域ID不能为空");
        }
        if(EmptyUtils.isEmpty(welfareOrderSaveDto.getFlags())) {
            return JsonResult.paramErrWithMsg("座位标识不能为空");
        }
        if(EmptyUtils.isEmpty(welfareOrderSaveDto.getCostPrice())) {
            return JsonResult.paramErrWithMsg("成本价不能为空");
        }
        if(EmptyUtils.isEmpty(welfareOrderSaveDto.getSalePrice())) {
            return JsonResult.paramErrWithMsg("销售价不能为空");
        }
        welfareOrderSaveDto.setEnterpriseId(getWxUserInfo().getEnterpriseId());
        welfareOrderSaveDto.setUserId(getWxUserId());
        welfareOrderSaveDto.setUserName(getWxUserInfo().getUserName());
        Integer orderId = service.createMovieOrder(welfareOrderSaveDto);
        return orderId > 0 ? JsonResult.success(orderId) : JsonResult.paramErrWithMsg("生成订单失败！");
    }

    /**
     * 获取电影订单支付卡默认匹配结果
     * @param orderId 订单id
     * @author liuguanjun
     * @date 2021-11-05 11:45:50
     **/
    @GetMapping(value = "getPayInfoByOrderId")
    public JsonResult<WelfareMovieOrderPayVo> getPayInfoByOrderId(Integer orderId) {
        return JsonResult.success(service.getPayInfoByOrderId(getWxUserId(),orderId));
    }

    /**
     * 电影订单支付
     * @param wlfareMovieOrderPayDto 订单id等传参
     * @date 2021-11-05 17:03:05
     * @author liuguanjun
     */
    @PostMapping("saveMovieOrderToPay")
    public JsonResult<String> saveMovieOrderToPay(@RequestBody WelfareMovieOrderPayDto wlfareMovieOrderPayDto)  {
        if(EmptyUtils.isEmpty(wlfareMovieOrderPayDto.getOrderId())) {
            return JsonResult.paramErrWithMsg("订单id不能为空");
        }
        Map resMap = service.saveMovieOrderToPay(getWxUserInfo(),wlfareMovieOrderPayDto);
        Integer resCode = Integer.parseInt(resMap.get("code").toString());
        String msg = resMap.get("msg").toString();
        if (resCode.equals(200)) {
            return JsonResult.success(resMap.get("orderId").toString());
        }
        return JsonResult.failWithMsg(msg);
    }

    /**
     * 微信支付回调接口
     * @date 2021-11-04 14:36:05
     * @author liuguanjun
     */
    @RequestMapping("weChatPayForApplet")
    public JsonResult<Map> weChatPayForApplet(Integer orderId) throws Exception {
        Map resMap = service.weChatPayForApplet(orderId,getWxUserInfo().getOpenId());
        Integer resCode = Integer.parseInt(resMap.get("code").toString());
        String msg = resMap.get("msg").toString();
        if (resCode.equals(200)) {
            return JsonResult.success(resMap);
        }
        return JsonResult.failWithMsg(msg);
    }


    /**
     * 微信支付回调接口
     * @date 2021-11-04 14:36:05
     * @author liuguanjun
     */
    @RequestMapping("updateSettlementNotify")
    public String updateSettlementNotify(HttpServletRequest request) throws Exception {
        return service.updateSettlementNotify(request);
    }

    /**
     * 电影购票取消订单
     * @param tradeno 订单号
     * @date 2021-11-04 14:36:05
     * @author liuguanjun
     */
    @GetMapping("cancelOrder")
    public JsonResult<String> cancelOrder(String tradeno)  {
        if(EmptyUtils.isEmpty(tradeno)) {
            return JsonResult.paramErrWithMsg("订单号不能为空");
        }
        Boolean result = service.cancelMovieOrder(tradeno,"取消订单");
        return result ? JsonResult.success("取消订单成功") : JsonResult.paramErrWithMsg("取消订单失败！");
    }

    /**
     * 获取用户有效期内未支付的订单
     * @param mobile 用户手机号
     * @date 2021-11-04 14:36:05
     * @author liuguanjun
     */
    @GetMapping("getUnpaidMovieOrder")
    public JsonResult<String> getUnpaidMovieOrder(String mobile)  {
        if(EmptyUtils.isEmpty(mobile)) {
            return JsonResult.paramErrWithMsg("用户手机号不能为空");
        }
        String tradeno = service.getUnpaidMovieOrder(mobile);
        return EmptyUtils.isNotEmpty(tradeno) ? JsonResult.success(tradeno) : JsonResult.paramErrWithMsg("无未支付订单！");
    }


    /**
     * 电影票订单列表分页查询
     * @date 2021/11/06 10:46
     * @author liuguanjun
     * @param query 查询条件
     */
    @GetMapping("/queryMovieOrderListPage")
    public PageUtils<WelfareMovieOrderVo> getWelfareMovieCinemaVoListPage(WelfareMovieOrderQuery query) {
        query.setUserId(getWxUserId());
        PageInfo<WelfareMovieOrderVo> pageInfo = service.getWelfareMovieOrderVoListPage(query);
        return new PageUtils<>(pageInfo);
    }


    /**
     * 获取电影票订单详情
     * @param orderId 电影票订单id
     * @author liuguanjun
     * @date 2021-11-06 10:46
     **/
    @GetMapping(value = "getMovieOrderDetail")
    public JsonResult<WelfareMovieOrderVo> getMovieOrderDetail(Integer orderId) {
        if(EmptyUtils.isEmpty(orderId)){
            return JsonResult.failWithMsg("缺少参数:电影订单id[orderId]");
        }
        return JsonResult.success(service.getMovieOrderDetail(orderId));
    }
}
